package com.sec.etech.ism.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sec.etech.ism.mapper.IsmTaskLogMapper;
import com.sec.etech.ism.model.IsmTaskLog;
import org.openbpm.base.core.id.IdUtil;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class IsmTaskLogService extends ServiceImpl<IsmTaskLogMapper, IsmTaskLog> {
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;//TaskExecutorConfig

    /**
     *
     * @param taskId
     * @param serviceCode
     * @param serviceName
     * @param methodName
     * @param url
     * @param inputJson
     * @param result
     * @return
     */
    public void saveLog(Long taskId, String serviceCode, String serviceName, String methodName, String url, String inputJson, JSONObject result){
        //保存接口调用日志
        /*JSONObject logObj = new JSONObject();
        logObj.put("task_id", taskId);
        logObj.put("service_code", serviceCode);
        logObj.put("service_name", serviceName);
        logObj.put("method_name", methodName);
        logObj.put("request_url", url);
        logObj.put("input_json", inputJson);
        logObj.put("output_json", result.getString("data"));
        logObj.put("message", result.getString("msg"));*/
        this.saveLog(taskId, serviceCode, serviceName, methodName, result.getString("msg"), url, inputJson,
                result.getString("data"));
    }

    /**
     * 保存接口交互日志信息
     * @param object
     */
    public Long saveLog(JSONObject object){
        IsmTaskLog ismTaskLog = JSONObject.parseObject(JSONObject.toJSONString(object), IsmTaskLog.class);
        return this.saveLog(ismTaskLog);
    }
    /**
     * 保存接口交互日志信息
     * @param taskId
     * @param serviceCode
     * @param serviceName
     * @param methodName
     * @param message
     * @param requestUrl
     * @param inputJson
     * @param outputJson
     */
    public Long saveLog(Long taskId, String serviceCode, String serviceName, String methodName,
                        String message, String requestUrl, String inputJson, String outputJson) {
        IsmTaskLog ismTaskLog = new IsmTaskLog();
        //ismTaskLog.setId(IdUtil.getUId());
        ismTaskLog.setTaskId(taskId);
        ismTaskLog.setServiceCode(serviceCode);
        ismTaskLog.setServiceName(serviceName);
        ismTaskLog.setMethodName(methodName);
        ismTaskLog.setMessage(message);
        ismTaskLog.setStatus(taskId!=null && taskId>0 ? 2 : 1);
        //ismTaskLog.setCreateTime(new Date());
        ismTaskLog.setRequestUrl(requestUrl);
        ismTaskLog.setInputJson(inputJson);
        ismTaskLog.setOutputJson(outputJson);
        return this.saveLog(ismTaskLog);
    }
    /**
     * 保存接口交互日志信息
     * @param ismTaskLog
     */
    public Long saveLog(IsmTaskLog ismTaskLog) {
        if(ismTaskLog.getId()==null) {
            ismTaskLog.setId(IdUtil.getUId());
        }
        ismTaskLog.setCreateTime(new Date());
        if(ismTaskLog.getTaskId()!=null && ismTaskLog.getTaskId()>0){
            ismTaskLog.setStatus(2);
        }else {
            ismTaskLog.setStatus(1);
        }
        final IsmTaskLog logTemp = ismTaskLog;
        threadPoolTaskExecutor.execute(()->{
            baseMapper.insert(logTemp);
        });
        return ismTaskLog.getId();
    }

    //保存因为程序异常导致的错误信息
    public void saveExceptionTaskLog(String serviceCode, String serviceName, Map<String, String> logMap){
        //保存接口调用日志
        JSONObject logObj = new JSONObject();
        logObj.put("service_code", serviceCode);
        logObj.put("service_name", serviceName);
        logObj.put("app_name", logMap.get("appName"));
        logObj.put("request_url", logMap.get("url"));
        logObj.put("input_json", logMap.get("input"));
        logObj.put("output_json", logMap.get("output"));
        logObj.put("message", "接口调用异常请联系管理员处理！");
        logObj.put("status", 2);
        this.saveLog(logObj);
    }
}
